package it.unibo.mumu;

import it.unibo.mumu.R;
import it.unibo.mumu.entity.Planet;
import it.unibo.mumu.util.SingletonSupport;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

public class AudioPlayerActivity extends Activity {
	private ImageButton playButton;
	private ImageButton pauseButton;
	private TextView audioTitleView;
	
	private MediaPlayer mp;
	
	private Planet planet;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_audio_player);
		
		int idUniverse = SingletonSupport.getInstance().getRealUniverseID();
		int idPlanet = SingletonSupport.getInstance().getRealPlanetID();
		
		planet = SingletonSupport.getInstance().getUniverses().get(idUniverse).getPlanets().get(idPlanet);
		
		if(SingletonSupport.getInstance().isHappiness()){
			// audio felice
			mp = MediaPlayer.create(this, Uri.parse(getFilesDir()+"/"+planet.getMultimediaHappy().getAudio().getAudioPath()));
		}else{
			// audio triste
			mp = MediaPlayer.create(this, Uri.parse(getFilesDir()+"/"+planet.getMultimediaSad().getAudio().getAudioPath()));
		}
		
		setAudioSource();
		playButton = (ImageButton) findViewById(R.id.audioPlayBtn);
		pauseButton = (ImageButton) findViewById(R.id.audioPauseBtn);
		audioTitleView = (TextView) findViewById(R.id.audioTitle);
		
		audioTitleView.setTextColor(Color.WHITE);
		
		if(SingletonSupport.getInstance().isHappiness()){
			audioTitleView.setText(planet.getMultimediaHappy().getAudio().getAudioTitle());
		}else{
			audioTitleView.setText(planet.getMultimediaSad().getAudio().getAudioTitle());
		}
		
		
		
		playButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				mp.start();
			}
		});
		
		pauseButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {

				mp.pause();
			}
		});
	}

	@Override
	protected void onPause() {
		mp.pause();
		super.onPause();
	}
	
	@Override
	protected void onDestroy() {
		mp.release();
		super.onDestroy();
	}
	
	private void setAudioSource() {
		try {

	        mp.prepare();
	        
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
		
	}
	
	public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        MenuItem item = menu.add(Menu.NONE, R.id.action_flip, Menu.NONE, R.string.action_back);
        item.setIcon( R.drawable.ic_action_info);
        item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
        return true;
    }
	
	public boolean onOptionsItemSelected(MenuItem item) {
		
		if(item.getItemId() == R.id.action_flip){
			
	        	Intent toInfo = new Intent(AudioPlayerActivity.this, InfoActivity.class);
	        	toInfo.putExtra("TITLE", "Informazioni sul cont. multimediale");
	        	
	        	if(SingletonSupport.getInstance().isHappiness()){
	        		toInfo.putExtra("TEXT", planet.getMultimediaHappy().getDescription());	
	        	}else{
	        		toInfo.putExtra("TEXT", planet.getMultimediaSad().getDescription());
	        	}
	        	
	        	
//				toPlanet.putExtra("PLANET_ID", ps.getIdPlanet());
				startActivity(toInfo);
	        
        }
        return super.onOptionsItemSelected(item);
    }
	
	@Override
	public void onBackPressed() {
		Intent toPlanetDetail = new Intent(AudioPlayerActivity.this,
				PlanetDetailActivity.class);
		System.gc();
		finish();
		startActivity(toPlanetDetail);
	}

}
